package com.library.admin.task;

import cn.hutool.core.thread.ThreadUtil;
import com.library.admin.modules.borrowing.service.BorrowingService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author: xyh
 * @create: 2023-08-18
 **/
@Component
public class BookTask {

    @Resource
    private BorrowingService borrowingService;

    @XxlJob("bookJobHandler")
    public void bookJobHandler() {
        XxlJobHelper.log("borrowing book start... ");
        try {
            //异步执行，超时最大30s
            ThreadUtil.execAsync(() -> borrowingService.scheduledNotice())
                    .get(30L, TimeUnit.SECONDS);
            XxlJobHelper.handleSuccess("图书借阅提醒用户信息执行完成！");
        } catch (Exception e) {
            XxlJobHelper.log("图书借阅提醒用户信息执行失败，错误信息：", e);
            XxlJobHelper.handleFail();
        }
    }
}
